package com.itheima.reggie.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.itheima.reggie.common.UserHolder;
import com.itheima.reggie.domain.Address;
import com.itheima.reggie.mapper.AddressMapper;
import com.itheima.reggie.service.AddressService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;


// 收货地址
@Service
@Transactional
public class AddressServiceImpl implements AddressService {
    @Autowired
    private AddressMapper addressMapper;

    //查看地址簿
    @Override
    public List<Address> findList() {
        // 构建条件
        LambdaQueryWrapper<Address> qw = new LambdaQueryWrapper<>();
        qw.eq(Address::getUserId, UserHolder.get().getId());
         // 因为肯定不是一条信息 是LIst
        List<Address> addresses = addressMapper.selectList(qw);
        //返回结果
        return addresses;
    }
     //新增
    @Override
    public void save(Address address) {
        // 补全信息
       address.setUserId(UserHolder.get().getId()); //id
        address.setIsDefault(0);
        // 返回结果
       addressMapper.insert(address);
    }
    // 修改默认地址
    @Override
    public void update(Long id) {
        // 先设置所有的地址都是非默认地址
        Address address = new Address();
        address.setIsDefault(0);
        // 在根据用户修改所有的地址
        LambdaQueryWrapper<Address> qw = new LambdaQueryWrapper<>();
        qw.eq(Address::getUserId,UserHolder.get().getId());
        addressMapper.update(address,qw);

        // 在根据前端传过来的id去修改默认地址
        Address address1=new Address();
        address1.setId(id);
        address1.setIsDefault(1);
        addressMapper.updateById(address1);


    }
    //结算
    @Override
    public Address jiesuan() {
        // 构建条件
        LambdaQueryWrapper<Address> qw = new LambdaQueryWrapper<>();
        qw.eq(Address::getIsDefault,1); //  查询默认地址为1
        qw.eq(Address::getUserId,UserHolder.get().getId());  // 根据id
        Address address = addressMapper.selectOne(qw);
        return address;
    }
    // 回显
    @Override
    public Address huixian(Long id) {
        //构建条件
        LambdaQueryWrapper<Address> qw = new LambdaQueryWrapper<>();
        qw.eq(Address::getId,id); // 条件查询
        Address address = addressMapper.selectOne(qw);
        return address;
    }
    // 修改
    @Override
    public void xiugai(Address address) {
        LambdaQueryWrapper<Address> qw = new LambdaQueryWrapper<>();
        qw.eq(Address::getUserId,UserHolder.get().getId());
        qw.eq(Address::getId,address.getId());
        addressMapper.delete(qw);
        // 先修改
        // 补全信息
        address.setUserId(UserHolder.get().getId()); //id
        address.setIsDefault(0);
        // 返回结果
        addressMapper.insert(address);



    }
    // 删除
    @Override
    public void delete(Long ids) {
        // 构建条件
        LambdaQueryWrapper<Address> qw = new LambdaQueryWrapper<>();
        qw.eq(Address::getId,ids);

        addressMapper.delete(qw);
    }
}
